const mysql = require('mysql');
import mySqlConnConfig from "../data_server/mySqlConnConfig";
import uuid from "node-uuid";

/*---Data Store 连接池---*/
const pool = mysql.createPool(mySqlConnConfig);

/*---sql 集合---*/
const sqls = {
    dbConnTest: () => {
        return `SELECT * FROM booksell.test;`;
    },
}

pool.on("connection", function() { console.log("创建一个连接") });
//当一个回掉压入队伍等待连接的时候触发入队事件
pool.on("enqueue", function() { console.log("入队") });

/*---销毁(释放)连接池---*/
const poolDestory = () => {}

// sql查询 公共响应
const sqlResponse = (err, res, callback) => {
    if (err) callback({ code: "err", errMsg: err });
    else callback({ code: "01", data: res });
}

/* @ 数据库连接测试
 **/
export const dbConnTest = (callback) => {
    pool.getConnection(function(error, connection) {
        let sql = `SELECT * FROM booksell.test;`;
        connection.query(
            sql,
            (err, rows) => {
                if (err) callback({ code: "err", errMsg: err });
                else callback({ code: "01", data: rows });
            }
        );
        connection.release(); // 释放连接
    });
}
